package com.ellricky.baselib.ui.order

import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import androidx.recyclerview.widget.RecyclerView
import com.andr.common.ui.fragment.BaseFragment
import com.andr.common.ui.mvp.BasePresenter
import com.andr.common.widget.refresh.RvRefreshListener
import com.ellricky.baselib.BaseConstant
import com.ellricky.baselib.R
import com.ellricky.baselib.cache.LocalCacheUtil
import com.ellricky.baselib.dialog.picker.EPickerUtil
import com.ellricky.baselib.entity.BaseData
import com.ellricky.baselib.entity.IntegralInfo
import com.ellricky.baselib.http.HttpUtils
import com.ellricky.baselib.util.EAppUtil
import com.ellricky.baselib.util.NewEBaseCallback
import com.ellricky.baselib.util.NewEListCallBack2
import kotlinx.android.synthetic.main.base_layout_class_integral.*

class IntegralDetailFragment : BaseFragment<Void, BasePresenter<Void>>(), RvRefreshListener,
    AdapterView.OnItemClickListener {

    private var paramMap: HashMap<String, String> = hashMapOf()
    private var dataSource: List<IntegralInfo> = arrayListOf()
    private var ePickerUtil: EPickerUtil? = null
    private var refresh = false

    private val adapter by lazy {
        IntegralAdapter()
    }

    private val memberId by lazy {
        arguments?.getString(BaseConstant.KEY_ID, "") ?: ""
    }

    companion object {
        fun newInstance(bundle: Bundle?): IntegralDetailFragment {
            val fragment = IntegralDetailFragment()
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun getContentLayout(): Int {
        return R.layout.base_layout_class_integral
    }

    override fun initUIView(p0: View?) {
        tv_title.text = "积分明细"

        rvView.init(this)

        ePickerUtil = EPickerUtil(requireActivity())

        initListener()

        loadDataOnInit()
    }

    private fun initListener() {
        ib_back.setOnClickListener {
            activity?.finish()
        }
    }

    private fun initParamMap() {
        paramMap["action"] = "integralList"
        paramMap["eid"] = LocalCacheUtil.getUid()
        paramMap["member_id"] = memberId
        paramMap["page"] = rvView.getCurPage().toString()
        paramMap["page_size"] = "40"
    }

    private fun loadDataOnInit() {
        rvView.loadContent(true)
    }

    override fun setAdapter(rv: RecyclerView?) {
        rv?.adapter = adapter
    }

    override fun loadData(refresh: Boolean) {
        this.refresh = refresh
        val url = EAppUtil.getApiHost()
        initParamMap()
        val callback: NewEBaseCallback = getResponseCallback2()
        HttpUtils.getInstance().postFrom(url, paramMap, callback)
    }

    private fun getResponseCallback2(): NewEBaseCallback {
        return object : NewEListCallBack2<IntegralInfo>(requireActivity()) {
            override fun onSuccess(
                data: MutableList<IntegralInfo>,
                baseData: BaseData<IntegralInfo>,
                msg: String?
            ) {
                dataSource = data
                rvView.onLoadFinish(data, refresh)
                tv_integral.text = "积分总数：${baseData.point_total}"
            }
        }
    }


    override fun updateAdapterData(p0: Boolean) {
        if (refresh) adapter.refresh(dataSource)
        else adapter.loadMore(dataSource)
    }

    override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {

    }
}